﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.IoC;
using Kooboo.Domain;
using System.Web;
using Kooboo.Commerce.Contexts.ShopSettings;

namespace Kooboo.Commerce
{
    public static class CategoryCommand
    {
        public static void Add(this ICategory parent, ICategory child)
        {
            child.Parent = parent;
            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                var repository = ContextContainer.Current.Resolve<ICategoryRepository>();
                repository.Post(child);
                transaction.Commit();
            }
        }

        public static void MoveTo(this ICategory child, ICategory parent)
        {
            var repository = ContextContainer.Current.Resolve<ICategoryRepository>();

            var item = repository.Categories.Where(i => i.Id == child.Id).FirstOrDefault();
            item.Parent = parent;

            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
             
                repository.Put(child);
                transaction.Commit();

                
            }
            
        }

        public static void Include(this ICategory category, IProduct product)
        {
            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                var repository = ContextContainer.Current.Resolve<ICategoryRepository>();
            
                repository.Include(category, product);
                transaction.Commit();
            }
   
        }

        public static void Exclude(this ICategory category, IProduct product)
        {
            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                var repository = ContextContainer.Current.Resolve<ICategoryRepository>();
                repository.Exclude(category, product);
                transaction.Commit();
            }
         
        }

        public static string BuildPhotoUrl(this ICategory target)
        {
            if (!String.IsNullOrEmpty(target.Photo))
            {
                return Configuration.AppRootPath.Replace("\\", "/") + Configuration.ImageFolderName + "/" + Configuration.CategoryPhotoFolder + "/" + HttpUtility.UrlEncode(target.Photo);
            }
            else 
            {
                //default image
                return "/images/no-image.jpg";
            }
        }

        public static string BuildThubmailPhotoUrl(this ICategory target)
        {
            if (!String.IsNullOrEmpty(target.Photo))
            {
                var host = new ServiceHost();
                return Configuration.AppRootPath.Replace("\\", "/") + Configuration.ImageFolderName + "/" + Configuration.CategoryPhotoFolder + "/" + host.GetShopSettings().ImageSettings.Thumbnail.Name + "/" + HttpUtility.UrlEncode(target.Photo);
            }
            else
            {
                //default image
                return "/images/no-image.png";
            }
        }
    }
}
